from rest_framework import serializers

from api.models import Porduct, PorductImage, StockRecord, StockPrice
from api.models.order import Order, OrderLine
from api.models.user import ShipAddress
from api.utils.serializers import PaginationSerializer, PaginationResultSerializer


class OrderUpdateAddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = ShipAddress
        fields = [
            "province",
            "city",
            "area",
            "detailed",
        ]

        extra_kwargs = {
            "province": {"required": False},
            "city": {"required": False},
            "area": {"required": False},
            "detailed": {"required": False},
        }


class OrderIdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = ["id"]


class OrderQeurySerializer(PaginationSerializer, serializers.ModelSerializer):
    product = serializers.IntegerField(help_text="商品ID", default=0, required=False)
    title = serializers.CharField(help_text="商品title", required=False)
    username = serializers.CharField(help_text="用户名", max_length=256, required=False)

    class Meta:
        model = Order
        fields = [
            "per_page",
            "page",
            "username",
            "limit",
            "status",
            "product",
            "title",
        ]

        extra_kwargs = {
            "status": {"required": False},
        }


class PirceOrderBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = StockPrice
        fields = "__all__"


class StockRecordToOrderLineBodySerializer(serializers.ModelSerializer):
    price = PirceOrderBodySerializer(many=True)

    class Meta:
        model = StockRecord
        fields = "__all__"


class OrderLineFileOfProduct(serializers.ModelSerializer):
    class Meta:
        model = PorductImage
        fields = "__all__"


class ProductToOrderLineSerializer(serializers.ModelSerializer):
    stock_record = StockRecordToOrderLineBodySerializer(many=True)
    images = OrderLineFileOfProduct(many=True)
    videos = OrderLineFileOfProduct(many=True)

    class Meta:
        model = Porduct
        fields = "__all__"


class OrderLineBodySerializer(serializers.ModelSerializer):
    prodcut = ProductToOrderLineSerializer()

    class Meta:
        model = OrderLine
        fields = "__all__"


class OrderBodySerializer(serializers.ModelSerializer):
    order_lines = OrderLineBodySerializer(many=True)
    address = OrderUpdateAddressSerializer()

    class Meta:
        model = Order
        fields = [
            "id",
            "order_num",
            "user",
            "address",
            "status",
            "cancel",
            "order_lines",
            "total_rmb",
        ]


class OrderDataSerializer(PaginationResultSerializer):
    data = OrderBodySerializer(many=True)
